package rm1;

import java.nio.charset.Charset;
import java.util.PriorityQueue;


public class Message implements  Comparable<Message>{
	/** General Declaration */
	private String CustomerID;
	private String ItemID;
	private int NumberOfItems;
	private String BoughtItemID;
	private int BoughtNumber;
	private long SequencerID;
	private String RequestType;// buy,return,checkstok,exchange
		
	public Message() {
		// default constructor
	}
	
	public Message(int i) {
		setInitial();
	}
	
	public void setInitial(){
		CustomerID = "M12345";
		ItemID ="1111";
		NumberOfItems = 0;
		BoughtItemID = "1111";
		BoughtNumber = 0;
		SequencerID = 0;
		RequestType = "checkStock";
	}
	
	public int bytoToInt(byte[] receivedMessage){
		int sizeOfString;
		String received = new String(receivedMessage, Charset.defaultCharset());
//		System.out.println("Reply recd.: "+received);
		sizeOfString = received.length();
//		System.out.println("size: "+sizeOfString);//gives the size of byte[]
		int n=0; //size of int in reply
		for (int i=0;i<sizeOfString;i++){
			if(Character.isDigit(received.charAt(i))){
				n++;
			}
		}
		
		//getting message in the default character
		String reply = new String(receivedMessage, 0, n, Charset.defaultCharset());
//		System.out.println("Reply after convertion: " + reply);
		return Integer.parseInt(reply);
	}
	
	/**
	 * this method is used to genrate the message that will be sent
	 * the message contents are seperated using space 
	 * @return
	 */
	public byte[] genrateMessage() {
		String messageString = " ";

		messageString = messageString + this.getCustomerID() + " ";
		messageString = messageString + this.getItemID() + " ";
		messageString = messageString + this.getNumberOfItems() + " ";
		messageString = messageString + this.getBoughtItemID() + " ";
		messageString = messageString + this.getBoughtNumber() + " ";
		messageString = messageString + this.getSequencerID() + " ";
		messageString = messageString + this.getRequestType() + " ";
		return messageString.getBytes();
	}

	/**
	 * this method is used to parse the message
	 * @param msg
	 */
	public void parseMessage(byte[] msg) {

		//convert to string
		String toParse = new String(msg);
		
		//split the string
		String[] result = toParse.split("\\s");
		for (int i=1;i<result.length;++i){
			//System.out.println("result at: "+i+" "+result[i]);
			switch(i)
			{
			case 1:
				this.setCustomerID(result[i]);
				break;
			case 2:
				this.setItemID(result[i]);
				break;
			case 3:
				this.setNumberOfItems(Integer.parseInt(result[i]));
				break;
			case 4:
				this.setBoughtItemID(result[i]);
				break;
			case 5:
				this.setBoughtNumber(Integer.parseInt(result[i]));
				break;
			case 6:
				this.setSequencerID(Integer.parseInt(result[i]));
				break;
			case 7:
				this.setRequestType(result[i]);
				break;
			}
		}
	}


	public String getCustomerID() {
		return CustomerID;
	}
	public void setCustomerID(String customerID) {
		if(""!=customerID)
			CustomerID = customerID;
	}
	public String getItemID() {
		return ItemID;
	}
	public void setItemID(String itemID) {
		ItemID = itemID;
	}
	public int getNumberOfItems() {
		return NumberOfItems;
	}
	public void setNumberOfItems(int numberOfItems) {
		NumberOfItems = numberOfItems;
	}
	public String getBoughtItemID() {
		return BoughtItemID;
	}
	public void setBoughtItemID(String boughtItemID) {
		if(""!=boughtItemID)
			BoughtItemID = boughtItemID;
	}
	public int getBoughtNumber() {
		return BoughtNumber;
	}
	public void setBoughtNumber(int boughtNumber) {
		BoughtNumber = boughtNumber;
	}
	public long getSequencerID() {
		return SequencerID;
	}
	public void setSequencerID(long sequencerID) {
		SequencerID = sequencerID;
	}
	public String getRequestType() {
		return RequestType;
	}
	public void setRequestType(String requestType) {
		this.RequestType = requestType;
	}

	/** to test this class */
	public static void main(String [] argu) {
		PriorityQueue<Message> t = new PriorityQueue<Message>();
		Message m1 = new Message(0);
		Message m2 = new Message(0);
		Message m3 = new Message(0);
		Message m4 = new Message(0);
		Message m5 = new Message(0);
		Message m6 = new Message(0);
		m1.setSequencerID(10);
		t.add(m1);
		m2.setSequencerID(13);
		t.add(m2);
		m3.setSequencerID(30);
		t.add(m3);
		m4.setSequencerID(111);
		t.add(m4);
		m5.setSequencerID(3);
		t.add(m5);
		m6.setSequencerID(50);
		t.add(m6);
		
		System.out.println(t.peek().getSequencerID());
		t.poll();
		System.out.println(t.peek().getSequencerID());
		t.poll();
		System.out.println(t.peek().getSequencerID());
	}

	@Override
	public int compareTo(Message o){
		
		return (int) (getSequencerID() - o.getSequencerID());
	}
	
}

